package com.hxd.mall.utils.local

import android.app.Activity
import android.graphics.Color
import android.os.Build
import android.view.View
import com.hxd.mall.constant.HxdConstants.Cache_Key_Is_First_Open
import com.hxd.mall.constant.HxdConstants.openedDialog
import com.hxd.mall.constant.HxdConstants.openedHome
import com.hxd.mall.constant.HxdConstants.openedProductDetails
import com.hxd.mall.constant.HxdConstantsAccount.isLogin
import com.hxd.mall.constant.HxdConstantsAccount.token
import com.hxd.mall.constant.HxdConstantsRouterPath.PathAccount.path_login_input_phone
import com.hxd.mall.constant.HxdGlobalInfo
import com.hxd.mall.module.EventBean
import com.hxd.mall.utils.local.HxdSharePreferenceUtil.clearAllData
import com.hxd.mall.utils.local.HxdSharePreferenceUtil.getBooleanData
import com.hxd.mall.utils.local.HxdSharePreferenceUtil.getStringData
import com.hxd.mall.utils.local.HxdSharePreferenceUtil.saveBooleanData
import com.hxd.mall.utils.local.HxdSharePreferenceUtil.saveStringData
import com.hxd.mall.utils.local.NavigationUtil.routerNavigation
import com.hxd.mall.utils.open.KeyboardHelperUtil
import com.hxd.mall.utils.open.KeyboardUtil
import crossoverone.statuslib.StatusUtil
import org.greenrobot.eventbus.EventBus

object ActivityUtil {
    /**
     * 入侵式状态栏
     */
    fun statusBarTran(activity: Activity, isBlack: Boolean) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            StatusUtil.setUseStatusBarColor(activity, Color.parseColor("#00000000"))
            StatusUtil.setSystemStatus(activity, true, isBlack)
        }

//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//            val window = activity.window
//            // 有些情况下需要先清除透明flag
//            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
//            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
//            window.statusBarColor = Color.TRANSPARENT
//        }
//
//        if (Build.VERSION.SDK_INT >= 21) {
//            val window = activity.window
//            window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
//            window.statusBarColor = Color.TRANSPARENT
//        }
    }

    /**
     * 判断是否登录
     */
    fun jump(callback: () -> Unit) {
        if (isLogin) {
            callback()
        } else {
            routerNavigation(path_login_input_phone)
        }
    }

    //切换首页的index
    fun sendEventBus(eventBus: EventBean) {
        EventBus.getDefault().post(eventBus)
    }

    /**
     * 判断用户是否登录
     */
//    fun isLogin(): Boolean {
//        if (HxdConstantsAccount.userInfo == null) {
//            return false
//        }
//
//        if (!User_Free_For_Main_Page) {
//            return true
//        }
//
//        return false
//    }

    fun clearUserInfo() {
        //userInfo = null
        token = null
        isLogin = false
        //Upload_Risk_Info_Statue = false
        //saveStringData(Cache_Key_User_Info, "")

        //只保留是否是第一次打开的标记，其他信息全部清除，防止用户二次登陆别的账户泄漏之前账号的信息
        val isFirstOpen = getBooleanData(Cache_Key_Is_First_Open)
        val openedHomeContent = getStringData(openedHome)
        val openedProductDetailsContent = getBooleanData(openedProductDetails)
        val openedDialogContent = getBooleanData(openedDialog)
        clearAllData()
        saveBooleanData(Cache_Key_Is_First_Open, isFirstOpen)
        saveStringData(openedHome, openedHomeContent)
        saveBooleanData(openedProductDetails, openedProductDetailsContent)
        saveBooleanData(openedDialog, openedDialogContent)

        //解绑极光推送
        ThirdInitUtil.unBindJPush()
    }

//    fun saveUserInfo(token: String, it: UserInfo) {
//        it.token = token
//        userInfo = it
//
//        saveUserInfo()
//    }
//
//    fun saveUserInfo() {
//        saveStringData(Cache_Key_User_Info, GsonUtil.bean2json(HxdConstantsAccount.userInfo!!))
//    }

    fun setViewStatusChangeListener(
        view: View,
        mKeyboardHelper: KeyboardHelperUtil,
        otherView: View
    ): Int {
        val array = intArrayOf(0, 0)
        view.getLocationOnScreen(array)
        val height = HxdGlobalInfo.deviceHeight - array[1] - view.height
        mKeyboardHelper.setOnKeyboardStatusChangeListener(
            KeyboardUtil.getKeyBoardStatusChangeListener(
                height,
                otherView
            )
        )
        return height
    }
}
